package com.longrise.android.byjk.plugins.hra.hratabfirst;

import android.text.TextUtils;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.datasource.remote.LoadDataManager;

/**
 * Created by Administrator on 2018/1/21.
 */

public class HraOrderCheckPresenter extends HraOrderCheckContract.Presenter{


    @Override
    protected void init() {

    }

    //上传预约检测信息
    @Override
    public void uploadOrderCheck(EntityBean bean) {
        EntityBean bean1 = new EntityBean();
        bean1.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "hra_submitOrder", bean1, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    clientParse(result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished(String key, String service) {

            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

        });
    }


    //上传修改服务点信息
    @Override
    public void uploadEndorseInfo(EntityBean bean) {
        mView.setBtnEnable(false);
        EntityBean bean1 = new EntityBean();
        bean1.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "hra_modifyMyEndorseStore", bean1, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    clientEndorseParse(result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished(String key, String service) {
                mView.setBtnEnable(true);
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

        });
    }

    private void clientParse(Object result){
        EntityBean bean = (EntityBean) result;
        int state = bean.getInt("resultstate");
        if (state == 1) {
            if(!TextUtils.isEmpty(Control.getOrderCheckName())){
                Control.setOrderCheckName("");
            }

            if(!TextUtils.isEmpty(Control.getOrderCheckCard())){
                Control.setOrderCheckCard("");
            }

            if(!TextUtils.isEmpty(Control.getOrderCheckPhone())){
                Control.setOrderCheckPhone("");
            }

            if(!TextUtils.isEmpty(Control.getOrderCheckSex())){
                Control.setOrderCheckSex("");
            }

            EntityBean resultBean = bean.getBean("result");
            String orderid = resultBean.getString("orderid");
            String name = resultBean.getString("name");
            String cardno = resultBean.getString("cardno");
            String number = resultBean.getString("number");
            String price = resultBean.getString("price");
            mView.toPay(orderid,name,cardno,number,price);
        } else {
            mView.showToast(bean.getString("resultdesc"));
        }
    }


    private void clientEndorseParse(Object result){
        EntityBean bean = (EntityBean) result;
        int state = bean.getInt("resultstate");
        if (state == 1) {
            if(!TextUtils.isEmpty(Control.getOrderCheckName())){
                Control.setOrderCheckName("");
            }

            if(!TextUtils.isEmpty(Control.getOrderCheckCard())){
                Control.setOrderCheckCard("");
            }

            if(!TextUtils.isEmpty(Control.getOrderCheckPhone())){
                Control.setOrderCheckPhone("");
            }

            if(!TextUtils.isEmpty(Control.getOrderCheckSex())){
                Control.setOrderCheckSex("");
            }
            mView.showToast("改签成功");
            mView.start2DetailActivity();
        } else {
            mView.showToast(bean.getString("resultdesc"));
        }
    }


}
